var t = getApp(), i = require("../../A4730AC3A71FB5DFC21562C4223CA4D1.js"), a = require("../../0BACB9C3A71FB5DF6DCAD1C4BE4CA4D1.js"), e = require("../../C6EFF1F4A71FB5DFA08999F3BD3CA4D1.js").getCodeOptionsParams;

Page({
    data: {
        winWidth: t.globalData.windowWidth,
        isLogining: !1,
        isLogin: !1,
        curSize: "",
        curSrc: "",
        productId: null,
        left: [],
        right: [],
        list: [],
        sizeList: [],
        isShow: !1,
        sign: "",
        type: 0,
        addressData: null
    },
    onLoad: function(i) {
        var a = this, s = {};
        if (i.scene) {
            var o = decodeURIComponent(i.scene), n = e(o, "sign"), r = e(o, "type");
            s.sign = n || "", s.type = r ? 1 * r : 0;
        }
        var d = t.globalData.isLogin;
        s.isLogin = d, this.setData(s, function() {
            d ? a.getPictureList() : a.login();
        });
    },
    onUnload: function() {
        t.globalData.chooseAddressItem = null;
    },
    onShow: function() {
        null !== a._cb && (this.setData({
            isLogining: !1
        }), a._cb = null);
        var i = t.globalData.chooseAddressItem;
        i && this.setData({
            addressData: i
        });
    },
    login: function() {
        var i = this;
        t.globalData.isLogin || this.data.isLogining || this.setData({
            isLogining: !0
        }, function() {
            a.login(function(t) {
                i.setData({
                    isLogining: !1
                }), 1001 === t && i.setData({
                    isLogin: !0
                }, function() {
                    i.getPictureList();
                });
            });
        });
    },
    onShareAppMessage: function() {},
    getPictureList: function() {
        var t = this;
        i.get("/v1/currency/goods", {
            device_code: "miniprogram"
        }).then(function(i) {
            if (1001 === i.data.code) {
                var a = i.data.response.data;
                t.setData({
                    list: a.list || [],
                    sizeList: a.size || []
                }, function() {
                    t.formatPictureList();
                });
            }
        });
    },
    formatPictureList: function() {
        for (var t = this.data.list, i = 0, a = 0, e = [], s = [], o = 0; o < t.length; o++) {
            var n = t[o], r = n.product_coverimg.width, d = n.product_coverimg.height, c = JSON.parse(JSON.stringify(n));
            c.product_coverimg.width = 327, c.product_coverimg.height = 327 * d / r, i <= a ? (i += (c.product_coverimg.height || 0) + 102, 
            e.push(c)) : (a += (c.product_coverimg.height || 0) + 102, s.push(c));
        }
        this.setData({
            left: e,
            right: s
        });
    },
    changeSize: function(t) {
        this.setData({
            curSize: t.currentTarget.dataset.size
        });
    },
    submit: function() {
        var a = this;
        if (t.globalData.isLogin) {
            if (!this.data.isLoading) {
                var e = this.data.productId;
                if (e) {
                    var s = this.data.curSize;
                    if (s) {
                        var o = this.data.addressData;
                        o ? (this.setData({
                            isLoading: !0
                        }), wx.showLoading(), i.post("/v1/currency/order", {
                            product_id: e,
                            address_id: o.address_id,
                            size: s,
                            type: this.data.type,
                            user_id: t.globalData.user.user_id,
                            sign: this.data.sign
                        }).then(function(t) {
                            if (wx.hideLoading(), a.setData({
                                isLoading: !1
                            }), 1001 === t.data.code) {
                                var i = t.data.response.data;
                                wx.navigateTo({
                                    url: "/pages/pay/pay?from=activity&orderId=" + i.order_id + "&price=" + i.order_total_paid / 100
                                });
                            }
                        }).catch(function() {
                            wx.hideLoading(), a.setData({
                                isLoading: !1
                            });
                        })) : this.goToMyAddress();
                    } else wx.showToast({
                        title: "请选择衣服尺寸",
                        icon: "none"
                    });
                } else wx.showToast({
                    title: "请选择作品图案",
                    icon: "none"
                });
            }
        } else this.login();
    },
    previewImg: function(t) {
        this.setData({
            curSrc: t.currentTarget.dataset.src,
            isShow: !0
        });
    },
    hidePreviewBox: function() {
        this.setData({
            isShow: !1
        });
    },
    handleProduct: function(t) {
        this.setData({
            productId: 1 * t.currentTarget.dataset.id
        });
    },
    goToMyAddress: function() {
        t.globalData.isLogin ? wx.navigateTo({
            url: "/pages/address/index?from=order"
        }) : this.login();
    }
});